import request from "@/utils/request";
import { useUserStore } from '@/stores/user';
// 获取 token 的工具函数  
const getToken = () => {
    const userStore = useUserStore();
    return userStore.token;
};
const getUserid = () => {
    const userStore = useUserStore();
    return userStore.userInfo.id;
};

// 列表获取接口
export const getCardInfo = (params) => request.get("/essay/list", params, {
    headers: {
        'Authorization': getToken() // 获取用户 token  
    }
});

//搜索接口
export const getSearchInfo = (params) => request.post("/essay/search", params);

// 获取文章详情
export const getArticleDetail = (id,userId) => request.post("/essay/detail", null, {
    params: { id,userId }
});
//信息修改接口
export function saveChanges(data) {
    return request({
        url: '/user/updateUserInfo',
        method: 'put',
        data: data
    })
}
// 检查文章是否收藏  
export const checkCollectionApi = (articleId) =>
    request.get('/essay/isCollected', {
        params: { articleId }, // 将 articleId 作为查询参数传递
        headers: {
            'Authorization': getToken() // 获取用户 token  
        }
    });

// 切换收藏状态  
export const toggleCollectionApi = (articleId) =>
    request.post('/essay/collect', null, {
        params: { articleId },
        headers: {
            'Authorization': getToken() // 获取用户 token  
        }
    });
//获取收藏文章
export const getcollectionInfo = (params) =>
    request.get("/essay/collections", params, {
        headers: {
            'Authorization': getToken() // 获取用户 token  
        }
    });
// 获取浏览记录
export const getHistoryAPI = (params) =>
    request.get("/essay/history", params, {
        headers: {
            'Authorization': getToken() // 获取用户 token  
        }
    });


// 清空浏览记录
export const clearHistoryAPI = () =>
    request.delete("/essay/deleteHistry", null, {
        headers: {
            'Authorization': getToken() // 获取用户 token  
        }
    });
//获取点赞推文
export const getpostInfo = (params) =>
    request.get("/posts/collections", params, {
        headers: {
            'Authorization': getToken() // 获取用户 token  
        }
    });